﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Admx_Generator
{
    public class Reference : IEquatable<Reference>
    {
        /// <summary>
        /// Create a new instance of a Reference.
        /// </summary>
        /// <param name="namespacePrefix">The prefix, in the UsedNamepace, identifying the namespace where the Parent Category is declared.
        /// If the Parent Category and the child Category are declared in the same namespace, leave empty.</param>
        /// <param name="referencedName">The name of the Parent Category as it is declare in the Admx file.</param>
        public Reference(string namespacePrefix, string referencedName)
        {
            this.NamespacePrefix = namespacePrefix;
            this.ReferencedName = referencedName;
        }

        /// <summary>
        /// Create a new instance of a Reference from a full reference string.
        /// </summary>
        /// <param name="refValue">A full reference string as «NamespacePrefix:ParentCategoryName», like «windows:WindowsComponents», or «WindowsComponents» if the parent category is in the same Admx File.</param>
        public Reference(string refValue)
        {
            int index = refValue.IndexOf(':');
            if(index != -1)
            {
                this.NamespacePrefix = refValue.Substring(0, index);
                this.ReferencedName = refValue.Substring(index + 1);
            }
            else
            {
                this.NamespacePrefix = String.Empty;
                this.ReferencedName = refValue;
            }
        }

        public string NamespacePrefix { get; private set; }

        public string ReferencedName { get; private set; }

        public bool Equals(Reference other)
        {
            return other == null ? false : String.Compare(this.NamespacePrefix, other.NamespacePrefix, true) == 0 && String.Compare(this.ReferencedName, other.ReferencedName, true) == 0;
        }

        public override string ToString()
        {
            return String.Concat(this.NamespacePrefix, !String.IsNullOrEmpty(this.NamespacePrefix) ? ":" : String.Empty, this.ReferencedName);
        }
    }
}
